//
//  HJFaceView.m
//  XWC
//
//  Created by Hello on 2022/10/26.
//

#import "HJFaceView.h"
#import "HJFaceCell.h"
#import "HJFaceCellData.h"
#import "TUIDefine.h"
#import "HJCommonModel.h"

 
@interface HJFaceView () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSArray <HJFaceCellData *> *faceDatas;
 


@end

@implementation HJFaceView

- (id)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]){
        [self setupViews];
        [self loadData];
    }
    return self;
}

- (void)setupViews
{
    self.backgroundColor = UIColorMake(0xEBF0F6);

    _faceFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    _faceFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _faceFlowLayout.sectionInset = UIEdgeInsetsMake(TFaceView_Margin, TFaceView_Page_Padding, Bottom_SafeHeight, TFaceView_Page_Padding);
    _faceFlowLayout.itemSize = CGSizeMake(30, 30);

    NSInteger count = 8; 
    _faceFlowLayout.minimumLineSpacing = (KScreenWidth - _faceFlowLayout.sectionInset.left - _faceFlowLayout.sectionInset.right - count * _faceFlowLayout.itemSize.width) / (count - 1);;
    _faceFlowLayout.minimumInteritemSpacing =  _faceFlowLayout.minimumLineSpacing;

    _faceCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_faceFlowLayout];
    [_faceCollectionView registerClass:[HJFaceCell class] forCellWithReuseIdentifier:TFaceCell_ReuseId];
    _faceCollectionView.collectionViewLayout = _faceFlowLayout;
    _faceCollectionView.delegate = self;
 
    
    _faceCollectionView.dataSource = self;
    _faceCollectionView.showsHorizontalScrollIndicator = NO;
    _faceCollectionView.backgroundColor = self.backgroundColor;
    _faceCollectionView.alwaysBounceVertical = YES;
    [self addSubview:_faceCollectionView];

    _lineView = [[UIView alloc] init];
    _lineView.backgroundColor = UIColorMake(0xDBDBDB);
    
    UIButton *sendButton = [UIButton buttonWithSuperView:self font:UIFontMediumMake(16) color:UIColor.whiteColor title:@"发送"];
    [sendButton setBackgroundImage:[UIImage imageWithColor:[UIColor tj_mainColor]] forState:UIControlStateNormal];
    sendButton.addCornerRadius(5);
    [sendButton addTarget:self action:@selector(didClickSendButton) forControlEvents:UIControlEventTouchUpInside];
    [sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(12);
        make.width.offset(60);
        make.height.offset(40);
        make.bottom.inset(Bottom_SafeHeight);
    }];
    
    UIButton *deleteButton = [UIButton addToView:self];
    deleteButton.backgroundColor = [UIColor whiteColor];
    [deleteButton setImage:[UIImage imageNamed:@"del_normal"] forState:UIControlStateNormal];
    [deleteButton setImage:[UIImage imageNamed:@"del_pressed"] forState:UIControlStateHighlighted];
    deleteButton.addCornerRadius(5);
    [deleteButton addTarget:self action:@selector(didClickDeleteButton) forControlEvents:UIControlEventTouchUpInside];
    [deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(sendButton.mas_left).inset(16);
        make.bottom.width.height.equalTo(sendButton);
    }];
 }

- (void)didClickSendButton {
    if ([self.delegate respondsToSelector:@selector(faceViewDidSend:)]) {
        [self.delegate faceViewDidSend:self];
    }
    
}

- (void)didClickDeleteButton {
    if ([self.delegate respondsToSelector:@selector(faceViewDidBackDelete:)]) {
        [self.delegate faceViewDidBackDelete:self];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _lineView.frame = CGRectMake(0, 0, self.frame.size.width, TLine_Heigh);
    _faceCollectionView.frame = CGRectMake(0, _lineView.frame.origin.y + _lineView.frame.size.height, self.frame.size.width, self.frame.size.height  - _lineView.frame.size.height);
}
 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.faceDatas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HJFaceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:TFaceCell_ReuseId forIndexPath:indexPath];
    [cell setData:self.faceDatas[indexPath.row]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HJFaceCellData *cellData = self.faceDatas[indexPath.row];
    if(_delegate && [_delegate respondsToSelector:@selector(faceView:didSelectFaceCellData:)]){
        [_delegate faceView:self didSelectFaceCellData:cellData];
    }
 
}
 
 
- (void)loadData {
    //emoji group
    NSMutableArray *emojiFaces = [NSMutableArray array];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"emoji" ofType:@"plist"];
    NSArray *emojis = [NSArray arrayWithContentsOfFile:filePath];
    for (NSDictionary *dic in emojis) {
        HJFaceCellData *data = [[HJFaceCellData alloc] init];
        NSString *name = [dic objectForKey:@"face_name"];
//        NSString *path = [NSString stringWithFormat:@"emoji/%@", name];
//        NSString *localizableName = [TUIGlobalization g_localizedStringForKey:name bundle:@"TUIChatFace"];
        data.name = name;
        data.path = name;
        data.localizableName = @"";
//        [self addFaceToCache:data.path];
        [emojiFaces addObject:data];
    }
    self.faceDatas = emojiFaces.copy;
    [self.faceCollectionView reloadData];
}

@end
